package com.lzxm.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.axis.session.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.druid.util.StringUtils;
import com.lzxm.constant.ServerResponse;
import com.lzxm.po.User;
import com.lzxm.po.UserInfo;
import com.lzxm.service.UserInfoService;
import com.lzxm.service.UserService;

@Controller
public class UserContrller {

	@Resource
	private UserService userService;
	@Autowired
	private UserInfoService userinfoService;
	
	@RequestMapping("/login.do")
	public ModelAndView login(HttpServletRequest request){
		ModelAndView mv = new ModelAndView();
		String username = request.getParameter("username");
		String password = request.getParameter("password");
		
		if(StringUtils.isEmpty(username) || StringUtils.isEmpty(password)){
			mv.addObject("errorInfo", "用户名和密码不能为空！");
			mv.setViewName("login");
			return mv;
		}
		User user = userService.getUser(username, password);
		
		if(user == null){
			mv.addObject("errorInfo", "用户名或密码错误！");
			mv.setViewName("login");
			return mv;
		}
		HttpSession session = request.getSession();
		session.setAttribute("user", user);
	    int user_id=user.getUser_id();
	    
	    UserInfo userinfo=userinfoService.getUserInfo(1);
	  
		mv.addObject("user", user);
		mv.setViewName("Navigation");
		
		return mv;
	}
	
	@RequestMapping(value="/update/password.do")
	@ResponseBody
	public ServerResponse updataLogin(HttpServletRequest request){
		ServerResponse response;
		HttpSession session = request.getSession();
		User user = (User) session.getAttribute("user");
		if(user == null){
			response = ServerResponse.createByErrorMsg("请先登陆！");
			return response;
		}
		
		int user_id=user.getUser_id();
		
		String login_pass = request.getParameter("login_pass");
		String login_pass_check=request.getParameter("login_pass_check");
		
		if(StringUtils.isEmpty(login_pass) || StringUtils.isEmpty(login_pass_check)){
			response = ServerResponse.createByErrorMsg("密码不能为空！！");
			return response;
		}
		if(!login_pass.equals(login_pass_check)){
			response = ServerResponse.createByErrorMsg("密码不相同！");
			return response;
		}
		user.setLogin_pass(login_pass);
	    userService.updateLogin(user);
		
	    response = ServerResponse.createBySuccessMsg( "操作成功！");
		return response;
	}
	
}
